Passed
Push — master ( 5a3a56...9db9b4 )
by Magnus
01:28
created

socket.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 1
c 7
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
"use strict";
2
3
const WebSocket = require("ws");
4
5
const handleProtocols = (protocols /*, request */ ) => {
6
    console.info(`Incoming protocol requests '${protocols}'.`);
7
    for (var i = 0; i < protocols.length; i++) {
8 2
        if (protocols[i] === "text") {
9
            return "text";
10 2
        } else if (protocols[i] === "json") {
11
            return "json";
12
        }
13
    }
14
    return false;
15
};
16
17
let makeWss = (server) => {
18
    const wss = new WebSocket.Server({
19
        server: server,
20
        clientTracking: true, // keep track on connected clients
21
        handleProtocols: handleProtocols
22
    });
23
24
    return wss;
25
};
26
27
let broadcastExcept = (ws, data) => {
28
    let clients = 0;
29
30
    wss.clients.forEach((client) => {
0 ignored issues
show
Bug introduced by
The variable wss seems to be never declared. If this is a global, consider adding a /** global: wss */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31 4
        if (client !== ws && client.readyState === WebSocket.OPEN) {
32
            clients++;
33 2
            if (ws.protocol === "json") {
34
                let msg = {
35
                    data: data
36
                };
37
38
                client.send(JSON.stringify(msg));
39
            } else {
40
                client.send(data);
41
            }
42
        }
43
    });
44
    console.info(`Broadcasted data to ${clients} (${wss.clients.size}) clients.`);
0 ignored issues
show
Bug introduced by
The variable wss seems to be never declared. If this is a global, consider adding a /** global: wss */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
};
46
47
let socket = (server) => {
48
    const wss = makeWss(server);
49
50
51
    wss.on("connection", (ws /*, req*/ ) => {
52
        console.info("Connection received. Adding client.");
53
54
        ws.on("message", (message) => {
55
            broadcastExcept(ws, message);
56
        });
57
58
        ws.on("error", (error) => {
59
            console.error(`Server error: ${error}`);
60
        });
61
62
        ws.on("close", (code, reason) => {
63
            console.info(`Closing connection: ${code} ${reason}`);
64
        });
65
    });
66
};
67
68
module.exports = {
69
    socket: socket
70
};
71